var app = getApp();
var dateTimePicker = require('../../utils/dateTimePicker.js');
var date = new Date();
var currentYear = date.getFullYear();
var nextYear = currentYear + 1;

Page({

  data: {
    showgif:true,
    hArray: [
      {
        hou: 15,
        name: '冲床员工'
      },
      {
        hou: 17,
        name: '定钉员工'
      },
      {
        hou: 18,
        name: '整形员工'
      },
      {
        hou: 19,
        name: '压支架员工'
      },
      {
        hou: 20,
        name: '压铆钉员工'
      },
      {
        hou: 21,
        name: '焊袖套员工'
      },
      {
        hou: 22,
        name: '拉床员工'
      },
      {
        hou: 23,
        name: '打珠员工'
      },
      {
        hou: 24,
        name: '包装员工'
      }
    ],
    hindex: 0,
    rulerhou:'',
    hname:'请选择变更角色',
    date: '2018-10-01',
    time: '12:00',
    dateTimeArray2: null,
    dateTime2: null,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: '',
    endYear: '',
    typename:'提交变更申请',
    status:'',
    statusindex:"",
    choose:'',
    info:{},
    startDate:'请选择开始时间',
    endDate:'请选择结束时间',
    pageInfo: '',
    index: 0,
    eindex: 0,
    index2: 0,
    array: [],
    earray: [],
    array2: ['上班', '下班'],
    date: '',
    liyou:'',
    workz:'',
    date2:'',
    hou:''
  },

  onLoad: function () {

    var that = this;
    if (!wx.getStorageSync('openid')) {
      app.error("请登录后再操作");
      return false;
    }
    this.setData({
      startYear: currentYear,
      endYear: nextYear,
    });

      // 获取完整的年月日 时分秒，以及默认显示的数组
      var obj2 = dateTimePicker.dateTimePicker(currentYear, nextYear);
      var obj1 = dateTimePicker.dateTimePicker(currentYear, nextYear);
      // 精确到分的处理，将数组的秒去掉
      var lastArray = obj1.dateTimeArray.pop();
      var lastTime = obj1.dateTime.pop();
      var lastArray2 = obj2.dateTimeArray.pop();
      var lastTime2 = obj2.dateTime.pop();
         
      this.setData({
        dateTime2: obj2.dateTime,
        dateTimeArray2: obj2.dateTimeArray,
        dateTimeArray1: obj1.dateTimeArray,
        dateTime1: obj1.dateTime
      });

      that.getwork();
  },
//角色变更事件
bindPickerChange_hx(e) {
  console.log('picker发送选择改变，索引为', e.detail.value);
  　var rulerhou = this.data.hArray[e.detail.value].hou //根据索引去查对应的id
  　var hname = this.data.hArray[e.detail.value].name //根据索引去查对应的id
    console.log(hname)
  this.setData({   //给变量赋值
      hindex: e.detail.value,
      rulerhou:rulerhou,
      hname:hname
 })
},
//时间控件start
changeDate(e){
       this.setData({ date:e.detail.value});
},
changeTime(e){
       this.setData({ time: e.detail.value });
},
changeDateTime(e){
       this.setData({ dateTime: e.detail.value });
},
changeDateTime2(e) {
  var that = this;
  this.setData({
    dateTime2: e.detail.value,
    endDate: that.data.dateTimeArray2[0][that.data.dateTime2[0]]+"-"+that.data.dateTimeArray2[1][that.data.dateTime2[1]]+"-"+that.data.dateTimeArray2[2][that.data.dateTime2[2]]+" "+that.data.dateTimeArray2[3][that.data.dateTime2[3]]+":"+that.data.dateTimeArray2[4][that.data.dateTime2[4]]
  });


},
changeDateTime1(e) {
  var that = this;
  this.setData({
    dateTime1: e.detail.value, 
    startDate: that.data.dateTimeArray1[0][that.data.dateTime1[0]]+"-"+that.data.dateTimeArray1[1][that.data.dateTime1[1]]+"-"+that.data.dateTimeArray1[2][that.data.dateTime1[2]]+" "+that.data.dateTimeArray1[3][that.data.dateTime1[3]]+":"+that.data.dateTimeArray1[4][that.data.dateTime1[4]]
  });
},
changeDateTimeColumn(e){
       var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;   
       arr[e.detail.column] = e.detail.value;
       dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
       this.setData({
         dateTimeArray: dateArr,
         dateTime: arr
       });
}, 
changeDateTimeColumn1(e) {
       var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
       arr[e.detail.column] = e.detail.value;
       dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
       this.setData({ 
         dateTimeArray1: dateArr,
         dateTime1: arr
       });
},
changeDateTimeColumn2(e) {
  var arr = this.data.dateTime2, dateArr = this.data.dateTimeArray2;
  arr[e.detail.column] = e.detail.value;
  dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
  this.setData({ 
    dateTimeArray2: dateArr,
    dateTime2: arr
  });
},

//时间控件end
  formSubmit: function (event) {
    var that = this;
    var sdate = that.data.startDate;
    var edate = that.data.endDate;
    if(that.data.hname == "请选择变更角色"){
      app.error('请选择变更角色');
      return false;
    }

    if(sdate == "请选择开始时间"){
      app.error('请选择开始时间');
      return false;
    }
    if(edate == "请选择结束时间"){
      app.error('请选择结束时间');
      return false;
    }
    if(edate <= sdate){
      app.error('结束时间必须大于开始时间');
      return false;
    }
    var liyou = event.detail.value.liyou;
      if (!liyou) {
        app.error('请输入变更理由');
        return;
      } 
      that.setData({showgif:true});
    wx.request({
      url: app.apiUrl + '/api/index/huanbanshenqing',
      data: {
        kaishi:sdate,
        jieshu:edate,
        liyou:liyou,
        openid:wx.getStorageSync('openid'),
        ban:wx.getStorageSync('nowbc'),
        qian:this.data.workz,
        hou:this.data.statusindex,
        rulerqian:that.data.info.groupid,
        rulerhou:that.data.rulerhou
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
        that.setData({showgif:false});
        if(res.data.code == 0){
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });
    
        }else{
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });
          setTimeout(function () {
            wx.redirectTo({
              url: '/page/my/index',
            })
          }, 2000)
       
        }
   
      }
    })



 
 
  
  },

//改变事件结束
  changezt(){
    var that = this
    console.log(that.data.itemList);
    wx.showActionSheet({

      //目前只有这三种  按天-》计件 按时-》计件  计件-》按时
     

      success(res) {
        that.setData({ status: that.data.itemList[res.tapIndex]});
        that.setData({ choose: res.tapIndex});
        console.log(res.tapIndex)
     
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  diysub(){
    app.error("当前不能操作！");
    return false;
  },

  getwork(){
    var that = this
  wx.request({
    url: app.apiUrl + '/api/data/getworkchange',
    data: {
      openid:wx.getStorageSync('openid'),
      groupid: wx.getStorageSync('groupid'),
    },
    method: 'POST',
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    success: res=> {
      that.setData({showgif:false});
        if(res.data){
          var status = "";
          var statusindex = ""
          console.log(res.data.data)
          that.setData({ info: res.data.data});
          that.setData({ workz: res.data.data.is_hours});
          that.setData({ typename: res.data.data.typename});
          //目前只有这三种  按天-》计件 按时-》计件  计件-》按时
          if(res.data.data.is_hours == 1){
            status = "计件";
            statusindex = 2;
          }else if(res.data.data.is_hours == 2){
            status = "计时";
            statusindex = 1;
          }else{
            status = "计件";
            statusindex = 2;
          }
       
          that.setData({ status: status});
          that.setData({ statusindex: statusindex});
          if(res.data.data.type != 9){
            that.setData({ 
              startDate: res.data.data.start,
              endDate: res.data.data.end
            });
          }

        }
    }
  })
},

  bindKeyInput(e){
      console.log(e.detail.value)
      this.setData({ liyou: e.detail.value});
  },

  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },

  ebindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      edate: e.detail.value
    })
  },


  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  ebindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      eindex: e.detail.value
    })
  },

})